using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Drawing;

namespace TextSupport
{
	public class OutlineController
	{
		private Outline _outline;
		public Outline Outline
		{
			get { return _outline; }
			set { _outline = value; }
		}

		private OutlineView _view;
		public OutlineView View
		{
			get { return _view; }
			set { _view = value; }
		}

		public void Layout()
		{
			float ypos = 0;

			Graphics g = _view.Surface.GetGraphics();

			// Measure each expanded node
			foreach (OutlineNode node in _outline.ExpandedNodes)
			{
				node.Top = ypos;
				node.Measure(View.Surface, g);
				ypos += node.Height;
			}

			_view.height = ypos;
		}

		public OutlineController(Outline outline, OutlineView view)
		{
			_outline = outline;
			_view = view;

			// Create the initial set of outline nodes for the outline
			// that already exists.  Not sure if this should really be here.
			createOutlineNodes(Outline.RootXmlNode, 0);
		}

		void createOutlineNodes(XmlNode node, int depth)
		{
			XmlNodeList outlineXmlNodes = node.SelectNodes("outline");
			foreach (XmlNode outlineXmlNode in outlineXmlNodes)
			{
				OutlineNode outlineNode = new OutlineNode(_view.Surface, outlineXmlNode, depth);

				Outline.OutlineNodes.Add(outlineNode);

				// All expanded by default
				Outline.ExpandedNodes.Add(outlineNode);

				createOutlineNodes(outlineXmlNode, depth + 1);
			}
		}
	}
}
